<template>
	<view class="observe-content" @click="showTologin">
		<view class="observe-top">
			<rf-navbar title="孩子们" :back="false"></rf-navbar>
			<view class="top-select" style="color: green;">
				<view class="select-left" @click="sexClick">
					<view class="select-text" :style="{color: highlight}">{{showSexTitle==""?"性别":showSexTitle}}</view>
					<view class="select-icon">
						<u-icon name="arrow-down-fill" :color="highlight" size="12"></u-icon>
					</view>
				</view>
				<view class="select-right" @click="ageClick">
					<view class="select-text" :style="{color: highlight1}">{{showAgeTitle==""?"年龄":showAgeTitle}}</view>
					<view class="select-icon">
						<u-icon name="arrow-down-fill" :color="highlight1" size="12"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<view class="observe-bottom" v-if="data.length>0">

			<u-index-list :index-list="data.map(item => item.code)" customNavHeight="152">
				<template v-for="(item, index) in data">
					<u-index-anchor :text="data.map(item => item.code)[index]" bgColor="#F5F6FA"
						height="30"></u-index-anchor>
					<u-index-item>
						<!-- <u-index-anchor :text="data.map(item => item.code)[index]" bgColor="#F5F6FA"></u-index-anchor> -->
						<view class="list-cell" v-for="(cell, row) in item.list" @click="childDetails(cell)">
							<view class="cell-image">
								<rf-avatar :avatarUrl="cell.headSculpture" :gender="cell.childSex"></rf-avatar>
							</view>
							<view class="cell-text">
								<view class="cell-text-top">
									<text class="name">{{ cell.childName }}</text>
									<image class="icon-my" style="width: 35rpx;height: 35rpx; margin-left: 20rpx;"
										@click.stop="shareChild(cell)" v-if="cell.shareList && cell.shareList.length>0">
									</image>
								</view>
								<view class="cell-text-bom">{{getChildAge(cell)}}</view>
							</view>
							<view class="right" v-if="cell.releaseCount">
								<view
									style="width: 88rpx;height: 44rpx; border-radius: 22rpx; background-color: #EBF8EE; display: flex;justify-content: center;align-items: center;">
									<text
										style="color: #2DAE3D; font-size: 26rpx; display: block;">{{cell.releaseCount}}篇</text>
								</view>
							</view>
						</view>
					</u-index-item>
				</template>
			</u-index-list>
		</view>
		<view class="observe-bottom" v-else>
			<view
				style="background-color: #fff; width: 100%; height: 400px; display: flex; justify-content: center; align-items: center;">
				<u-empty mode="car" :marginTop="50" text="暂无数据" icon="/static/images/common/empty.png"></u-empty>
			</view>
		</view>
		<u-action-sheet :show="showSex" :actions="sexList" title="选择性别" round="12" @close="showSex = false"
			@select="sexSelect"></u-action-sheet>
		<!-- 选择年龄 -->
		<view class="popup">
			<u-picker :show="showAge" :columns="ageList" keyName="name" title="选择年龄" round="12"
				:closeOnClickOverlay="true" @cancel="showAge = false" @close="showAge=false" @confirm="ageSelect"
				confirmColor="#58C866"></u-picker>
		</view>


		<!-- 添加按钮 -->
		<view class="add icon-add" @click="addChild" @touchstart="handleMouseDown" @touchmove.stop="handleMouseMove"
			@touchend="handleMouseUp" :style="{ top: top + 'px', left: left + 'px'}">
		</view>

		<!-- 用户未注册 提示注册 -->
		<rf-alert :show="showRegister" @close="showRegister=false" @confirm="onClickRegister" title="提示" desc="您还未登录"
			confirmText="去登录"></rf-alert>


		<rf-shareChild :show="shareChildShow" :child="childInfo" @close="shareChildShow=false"
			@confirm="shareChildConfirm" @set="shareChildSet"></rf-shareChild>
		<rf-load :loading="loading" title="加载中..."></rf-load>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState,
	} from 'vuex'; //引入mapState

	import {
		getChildList,
	} from '@/api/user.js'
	import {
		from,
		timer,
		of,
		TimeoutError,
		merge,
	} from 'rxjs';
	import {
		retry,
		retryWhen,
		takeWhile,
		concatMap,
		filter,
		switchMap,
		takeUntil,
		tap,
		catchError,
		delay,
		timeout,
		finalize,
	} from 'rxjs/operators';

	export default {
		data() {
			return {
				loading: false, // 加载中提示
				data: [],

				sexList: [{
						name: '全部',
						value: ""
					},
					{
						name: '女',
						value: "1"
					},
					{
						name: '男',
						value: "0"
					}
				],
				ageList: [
					[{
						'name': '全部',
						'value': "",
					}, {
						'name': '半岁',
						'value': "0.5",
					}, {
						'name': '一岁',
						'value': "1",
					}, {
						'name': '一岁半',
						'value': "1.5",
					}, {
						'name': '两岁',
						'value': "2",
					}, {
						'name': '两岁半',
						'value': "2.5",
					}, {
						'name': '三岁',
						'value': "3",
					}, {
						'name': '三岁半',
						'value': "3.5",
					}, {
						'name': '四岁',
						'value': "4",
					}, {
						'name': '四岁半',
						'value': "4.5",
					}, {
						'name': '五岁',
						'value': "5",
					}, {
						'name': '五岁半',
						'value': "5.5",
					}, {
						'name': '六岁',
						'value': "6",
					}, {
						'name': '六岁半',
						'value': "6.5",
					}, {
						'name': '七岁',
						'value': "7",
					}]
				],
				childSex: "",
				childAge: "",
				showSexTitle: "",
				showAgeTitle: "",
				showSex: false,
				showAge: false,
				highlight: "#000",
				highlight1: "#000",

				showRegister: false, // 用户未注册提示

				shareChildShow: false,

				childInfo: null, // 孩子信息

				dragging: false, // 是否正在拖拽
				mouseX: 0, // 鼠标在元素内的X坐标
				mouseY: 0, // 鼠标在元素内的Y坐标
				left: 0, // 元素左上角距离父容器左侧的距离
				top: 0, // 元素左上角距离父容器顶部的距离

			};
		},
		onLoad() {
			this.left = this.screenWidth - 80;
			this.top = this.screenHeight - 180;

			// this.shareChildShow = true;
		},
		onShow() {
			if (!this.hasLogin) {
				this.data = []
				return
			}
			// this.getKidList()
		},
		computed: {
			...mapState(['userInfo', 'hasLogin']),
			getChildAge() {
				return (val) => {

					for (let i = 0; i < this.ageList[0].length; i++) {
						if (this.ageList[0][i].value == val.childAge) {
							return this.ageList[0][i].name;
						}
					}
				}

			},
		},
		onTabItemTap(e) {
			console.log("onTabItemTap----");
			// 点击tabbaritem键的触发，常用于刷新
			this.getKidList()
		},
		mounted() {
			let _that = this;
			uni.$on('refreshChildList', function(data) {
				// 触发更新后
				_that.getKidList()
			})
		},
		onUnload() {
			// 为了优化代码，可以加上移除事件，避免重复监听事件
			// 移除监听事件  
			uni.$off('refreshChildList');
		},
		methods: {
			shareChild(child) {
				// 弹出共管人
				this.childInfo = child;
				console.log("-----------------==");
				this.shareChildShow = true;
			},
			shareChildConfirm() {
				this.shareChildShow = false;
			},
			shareChildSet() {
				this.shareChildShow = false;
				uni.navigateTo({
					url: "/pages-sub/childShare/childShare"
				})
			},

			showTologin() {
				if (!this.hasLogin) {
					this.showRegister = true;
				}

			},
			// 确认去注册
			onClickRegister() {
				this.showRegister = false;
				uni.switchTab({
					url: "/pages/home/home"
				});

			},
			// 点击选择性别
			sexClick() {
				if (!this.hasLogin) {
					this.showRegister = true;
					return
				}
				this.showSex = true;

			},
			// 点击选择年龄
			ageClick() {
				if (!this.hasLogin) {
					this.showRegister = true;
					return
				}
				this.showAge = true;
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false;
				// console.log('close');
			},
			sexSelect(e) {

				this.showSexTitle = e.name
				this.childSex = e.value

				if (this.showSexTitle == "全部") {
					this.highlight = "#3a3d4b"
				} else {
					this.highlight = "#6FC671"
				}


				this.getKidList()
			},
			ageSelect(e) {

				this.showAge = false
				this.childAge = e.value[0].value;
				this.showAgeTitle = e.value[0].name;
				if (this.showAgeTitle == "全部") {
					this.highlight1 = "#3a3d4b"
				} else {
					this.highlight1 = "#6FC671"
				}

				this.getKidList()
			},
			// 添加孩子
			addChild() {
				if (!this.hasLogin) {
					this.showRegister = true;
					return
				}
				uni.navigateTo({
					url: '/pages-sub/addChild/addChild'
				});
			},
			// 孩子详情
			childDetails(event) {
				console.log("event: " + JSON.stringify(event));
				uni.navigateTo({
					url: '/pages-sub/childFile/childFile?childid=' + event.childId
				});

			},
			formatArray(arr) {
				return arr.map(item => {

					return {
						id: item.id,
						name: item.name, // 会员名称
						amount: item.amount, // 价格
						type: item.type,
						des: item.description,
						wordStatus: item.wordStatus, // 生成word 版记录
						childCount: item.childCount, // 管理幼儿数量
						aiStatus: item.aiStatus, // ai 自动分析
						duration: item.duration, // 会员时间
						recordStatus: item.recordStatus, //  语音观察记录
					}
				})
			},
			getKidList() {
				var params = {
					childSex: this.childSex,
					childAge: this.childAge,
					childName: ""
				}

				// 发送网络请求
				const request = from(getChildList(params))

				// 订阅网络请求结果
				request.subscribe({
					next: (res) => {
						console.log("res: " + JSON.stringify(res));
						this.data = res.data
					},
					complete: () => {
						this.loading = false;
					},
					error: (err) => {

					}
				});

				// 发送网络请求，并设定2秒超时
				request.pipe(
					timeout(500),
					finalize(() => {
						console.log('超时订阅结束');
					})
				).subscribe(
					res => {
						// 1秒内返回结果，输出
						this.loading = false;
						console.log('加载结束--', res);
					},
					err => {
						// 超时，输出
						this.loading = true;
						console.log('加载超时');
					}
				);

				// this.loading = true;
				// from(getChildList(params)).pipe(
				// 	retry(3)
				// ).subscribe({
				// 	next: (res) => {
				// 		console.log("res: " + JSON.stringify(res));
				// 		this.data = res.data
				// 	},
				// 	complete: () => {
				// 		this.loading = false;
				// 	},
				// 	error: (err) => {
				// 	}
				// })
			},
			handleMouseDown(event) {
				this.dragging = true;
				this.mouseX = event.changedTouches[0].clientX;
				this.mouseY = event.changedTouches[0].clientY;
			},
			handleMouseMove(event) {
				if (this.dragging) {
					console.log("event", event);
					console.log("screenWidth", this.screenWidth);
					console.log("screenHeight", this.screenHeight);
					const deltaX = event.changedTouches[0].clientX - this.mouseX;
					const deltaY = event.changedTouches[0].clientY - this.mouseY;

					var left = this.left + deltaX;
					var top = this.top + deltaY;
					if (left <= 0) {
						this.left = 0
					} else if (left >= (this.screenWidth - 60)) {
						this.left = (this.screenWidth - 60)
					} else {
						this.left = left;
					}
					if (top <= 0) {
						this.top = 0
					} else if (top >= (this.screenHeight - 100)) {
						this.top = (this.screenHeight - 100)
					} else {
						this.top = top;
					}
					this.mouseX = event.changedTouches[0].clientX;
					this.mouseY = event.changedTouches[0].clientY;
				}
			},
			handleMouseUp(event) {
				console.log(event);
				this.dragging = false;
			},

		}
	};
</script>

<style lang="scss">
	.u-index-anchor {
		background-color: #f5f6fa !important;
	}

	.observe-content {
		min-height: calc(100vh);
		background-color: #fff;

		.observe-top {
			position: fixed;
			z-index: 999;
			width: 100%;
			height: 152px;
			background: url('../../static/icons/toubu.png');
			background-repeat: no-repeat;
			background-size: 100%;


			.top-text {
				// margin: 110rpx 326rpx 110rpx 328rpx;
				font-size: 32rpx;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #3a3d4b;
				position: absolute;
				top: 110rpx;
				left: 328rpx;
				// text-align: center;
				// line-height: 264rpx;
			}

			.top-select {
				display: flex;
				position: absolute;
				bottom: 28rpx;
				left: 32rpx;
				font-size: 28rpx;
				font-family: AppleSystemUIFont;
				color: #3a3d4b;

				.select-left {
					display: flex;
					align-items: center;

					.select-text {
						font-size: 28rpx;
						font-family: AppleSystemUIFont;
						// color: #3a3d4b;
						margin-right: 20rpx;
					}

					.select-icon {
						// color: #000;
						margin-right: 110rpx;
					}

					&.normal {}

					&.highlight {
						color: rgba(119, 198, 113, 1);
					}
				}

				.select-right {
					display: flex;
					align-items: center;

					.select-text {
						font-size: 28rpx;
						font-family: AppleSystemUIFont;
						color: #3a3d4b;
						margin-right: 20rpx;
					}

					.select-icon {
						color: #000;
						margin-right: 110rpx;
					}
				}
			}
		}

		.observe-bottom {
			height: calc(100vh - 264rpx);
			background-color: #f5f7f9;
			position: relative;
			top: 152px;

			.list-cell {
				display: flex;
				align-items: center;
				box-sizing: border-box;
				width: 100%;
				height: 148rpx;
				padding: 20px 40rpx;
				overflow: hidden;
				color: #323233;
				font-size: 14px;
				line-height: 28rpx;
				background-color: #fff;

				.cell-image {
					width: 100rpx;
					height: 100rpx;
					background-repeat: no-repeat;
					background-size: 100%;
					margin-right: 24rpx;

					.cell-icon {
						position: absolute;
						right: 0;
						bottom: 0;
					}
				}

				.cell-text {
					.cell-text-top {
						display: flex;
						align-items: center;

						.name {
							font-size: 32rpx;
							font-family: PingFangSC-Semibold, PingFang SC;
							font-weight: 600;
							color: #3a3d4b;
						}
					}

					.cell-text-bom {
						margin: 10rpx 0rpx;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #77798d;
					}
				}

				.right {
					flex: 1;
					display: flex;
					justify-content: flex-end;
				}
			}


		}

		// .popup {
		// 	width: 100%;
		// 	height: 626rpx;
		// 	background: #ffffff;
		// 	border-radius: 24rpx 24rpx 0rpx 0rpx;
		// }

		.add {
			position: fixed;
			right: 20px;
			bottom: 40px;
			z-index: 999;

			width: 108rpx;
			height: 108rpx;

			cursor: move;
			user-select: none;
		}

		.popup {
			.u-popup__content.data-v-3a231fda {
				position: relative;
				border-radius: 24rpx 24rpx 0 0;
				overflow: hidden;
				// padding-bottom: 150rpx;
			}

			.u-picker__view.data-v-f45a262e {
				height: 360rpx !important;
			}


			// .u-toolbar__wrapper__cancel.data-v-55c89db1 {
			// 	padding: 0;
			// 	position: absolute;
			// 	left: 172rpx;
			// 	bottom: -78rpx;
			// }

			// .u-toolbar__wrapper__confirm.data-v-55c89db1 {
			// 	padding: 0;
			// 	position: absolute;
			// 	right: 172rpx;
			// 	bottom: -78rpx;
			// }
		}
	}
</style>